package Dao.Process.Imp;

import Dao.Process.ReportDao;
import Entity.Process.Medium;
import Entity.Process.Report;
import Util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ReportDaoImp implements ReportDao {
    private PreparedStatement ps = null;
    private ResultSet rs = null;


    @Override
    public Report getReportBySNO(String SNO, Connection conn) {
        Report report = null;
        try {
            String sql = "select " +
                    "report_id, title, collname, mname, tno, sno, state , opinion ,is_pass " +
                    "from report  " +
                    "where sno = ? ";
            ps = conn.prepareStatement(sql);
            ps.setString(1, SNO);
            rs = ps.executeQuery();
            if (rs.next()) {
                Integer reportId = rs.getInt(1);
                String title = rs.getString(2);
                String collName = rs.getString(3);
                String mName = rs.getString(4);
                String tno = rs.getString(5);
                String sno = rs.getString(6);
                String state = rs.getString(7);
                String opinion = rs.getString(8);
                String isPass = rs.getString(9);
                report = new Report(reportId, title, collName, mName, tno, sno, state, opinion, isPass);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return report;
    }

    @Override
    public Integer updateCheck(Integer id, String result, String opinion, Connection conn) {
        int res = 0;
        try {
            String sql = "update report set state = '审核完成' , opinion = ? , is_pass = ? where report_id = ? ";
            ps = conn.prepareStatement(sql);
            ps.setString(1, opinion);
            ps.setString(2, result);
            ps.setInt(3, id);
            res = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return res;
    }

    @Override
    public Integer getSumByTNO(String tno, Integer type, Connection conn) {
        int res = 0;
        try {
            String sql;
            if (type == 1) {
                sql = "select COUNT(sno) from report where tno = ? ";
            } else {
                sql = "select COUNT(sno) from report where tno = ? and is_pass = '通过' ";
            }
            ps = conn.prepareStatement(sql);
            ps.setString(1, tno);
            rs = ps.executeQuery();
            if (rs.next()) {
                res = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeMySQLResource(ps, rs);
        }
        return res;
    }
}
